from django import forms
from rest_framework import serializers  # 导入序列化器

from .models import Course
from django.contrib.auth.models import User
class CourseSerializer(serializers.ModelSerializer):
    teacher = serializers.ReadOnlyField(source='teacher.username')  # 外键字段、只读

    class Meta:
        model = Course  # 写法和上面的CourseForm类似
        # exclude = ('id', ) # 注意元组中只有1个元素时不能写成("id")
        # fields = ('name', 'introduction', 'teacher', 'price')
        fields = "__all__"
        depth = 2  # 深度

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = "__all__"

# class CourseSerializer(serializers.HyperlinkedModelSerializer):
#     teacher = serializers.ReadOnlyField(source='teacher.username')  # 外键字段、只读
#
#     class Meta:
#         model = Course  # 写法和上面的CourseForm类似
#         # url是默认值，可在settings.py中设置URL_FIELD_NAME使全局生效
#         # fields = ('id', 'url', 'name', 'introduction', 'teacher', 'price', 'created_at', 'update_at')
#         fields = ('id', 'link', 'name', 'introduction', 'teacher', 'price', 'created_at', 'update_at')
